# -*- encoding=utf8 -*-
__author__ = "Bryan.Chen"

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.std import StdPoco
poco = StdPoco()

ST.OPDELAY = 0;

class Shop_AddDiamond:

    def __init__(self):
        print("%s __init__" % (__file__))
    
    def runTest(self, args):
        
        poco("HjLobbyScene").wait_for_appearance()
        
        btnAddDiamond = poco("node_blueDiamond").child("diamond_add")
        txtDiamond = poco("node_blueDiamond").child("txt_luckcoin_num").get_text() # 钻石数量 
        
        btnAddDiamond.click([0.5, 0.5])

        poco("LbPurchaseLayer").wait_for_appearance()
        
        lbPurchaseLayer = poco("LbPurchaseLayer")
        
        if not lbPurchaseLayer.exists():
            # 超时没有弹出充值框
            return True

        btnSelectProp = lbPurchaseLayer.offspring("select_btn2")
        btnSelectProp.click([0.5, 0.5])
        
        boughtItems = set()
        
        items = lbPurchaseLayer.offspring("item_list").offspring("item1")
        for item in items:
            itemName = item.child("item_name").get_text()

            if itemName not in boughtItems:
                item.child("item_price_bg").click([0.5, 0.5])

                poco("Panel_AskBuy").wait_for_appearance()

                askBuyPanel = poco("Panel_AskBuy")

                if not askBuyPanel.exists():
                    print("超时不弹出购买框")
                    break

                btnAskBuySure = askBuyPanel.offspring("btn_AskBuy_Sure")
                btnAskBuySure.click([0.5, 0.5])

                poco("PayChoseTipLayer").wait_for_appearance()

                payChoseTipLayer = poco("PayChoseTipLayer")
                if not payChoseTipLayer.exists():
                    print("超时不弹出选择支付方式框")
                    break
                    
                btnWechat = payChoseTipLayer.offspring("Button_1")
                btnWechat.click([0.5, 0.5])
                
                sleep(3.0)

                snapshot("点击微信支付后弹出的界面")

                keyevent("BACK")

                boughtItems.add(itemName)

            else:
                print("")
                
            break # 买一件商品就可以了

        
        sleep(1.0)
        
        poco("LbPurchaseLayer").wait_for_appearance()
        
        btnClose = lbPurchaseLayer.offspring("return_btn")
        btnClose.click([0.5, 0.5])
        
        return True
    
_instance = Shop_AddDiamond()                       
runTest = _instance.runTest

